#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2024/4/3 18:39
# @Author : 闫峰
# @File    : monitor_jdwalmart_listing_nosales.py
from common.setting import ensure_path_sep
from utils.mysqlUtils.db_pool import Database

db_java_test = Database(ensure_path_sep("\\config\\shopeev2_config.yaml.bak"), "test_jdw_database")


class MonitorJdwalmartListingNosales:

    @staticmethod
    def delete_data():
        db_java_test = Database(ensure_path_sep("\\config\\shopeev2_config.yaml.bak"), "test_jdw_database")
        """
        静态方法，用于删除指定数据库中的数据。

        参数:
        无

        返回值:
        无
        """
        # 定义删除操作的SQL语句
        delete_sql = """
            delete from monitor.monitor_jdwalmart_listing_nosales
        """
        # 执行SQL语句，并获取影响的行数
        rows = db_java_test.execute(delete_sql, ())
        # 打印删除操作的结果
        print(f"删除了数据表monitor_jdwalmart_listing_nosales{rows}条数据")

    @staticmethod
    def batch_insert_data(insert_data:list) -> int:

        # 构造插入数据的SQL语句，并执行插入操作
        insert_sql = """
        REPLACE INTO monitor.monitor_jdwalmart_listing_nosales 
        (source_id, source_name,item_id,source_sku, sku, create_time, update_time)
        VALUES (%s,%s,%s,%s,%s,now(),now());
        """
        rows = db_java_test.execute_many(insert_sql, insert_data)
        print(f"插入了 {rows}")
        return rows

    @staticmethod
    def get_text()->str:
        db_java_test = Database(ensure_path_sep("\\config\\shopeev2_config.yaml.bak"), "test_jdw_database")
        """
        发送钉钉消息，用于提醒Shopee平台禁售产品的数量。

        :return: 无返回值
        """
        # 查询monitor_shopee_listing_nosales表中需要发送的数据数量
        get_data_list_sql = """
            select count(1) as num from monitor.monitor_jdwalmart_listing_nosales
        """
        data_list = db_java_test.fetch_all_tuple(get_data_list_sql, ())
        if not len(data_list):
            count = 0
        count = data_list[0][0]  # 获取禁售产品数量

        # 构造钉钉消息文本
        text = f"---  \n\n" \
               f"jdwalmart平台禁售产品数：<font color='red' size='5'>{count}</font>  \n" \
               f"---  \n\n"
        return text